package com.think.admin.web.filter;

import com.think.admin.wrapper.RequestWrapper;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * 过滤器
 * </p>
 *
 * @author: xwj
 * @data: 2019-04-21
 */
@Component
@Order(Integer.MIN_VALUE)
public class AdminFilter implements Filter {

    @Override
    @SuppressWarnings("EmptyMethod")
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
                         FilterChain chain) throws ServletException, IOException {
        // 跨域支持
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers," +
                " Authorization, X-Requested-With, Access-Control-Allow-Methods, Access-Control-Allow-Origin");
        response.setHeader("Access-Control-Allow-Methods", "OPTIONS,GET,POST,PUT,DELETE");

        // xss
        RequestWrapper requestWrapper = new RequestWrapper((HttpServletRequest) req);

        // 执行
        chain.doFilter(requestWrapper, res);
        /*HttpServletRequest httpRequest = (HttpServletRequest) req;
        String uri = httpRequest.getRequestURI();
        System.err.println(uri);

        if(!uri.contains("/wechat")){
            // 跨域支持
            HttpServletResponse response = (HttpServletResponse) res;
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers," +
                    " Authorization, X-Requested-With, Access-Control-Allow-Methods, Access-Control-Allow-Origin");
            response.setHeader("Access-Control-Allow-Methods", "OPTIONS,GET,POST,PUT,DELETE");

            // xss
            RequestWrapper requestWrapper = new RequestWrapper((HttpServletRequest) req);

            // 执行
            chain.doFilter(requestWrapper, res);
        }else{
            // 执行
            chain.doFilter(req, res);
        }*/
    }

    @Override
    @SuppressWarnings("EmptyMethod")
    public void init(FilterConfig config) {
    }

}
